package com.common;

/**
 * Created by sola on 2018/1/28.
 */

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@Deprecated
public abstract class Chunk<T> implements Slice<T>, Serializable {
    private static final long serialVersionUID = 867755909294344406L;
    private final List<T> content = new ArrayList();
    private  Pageable pageable;

    public Chunk() {

    }
    public Chunk(List<T> content) {
        Assert.notNull(content, "Content must not be null!");
        this.content.addAll(content);
    }
    public Chunk(List<T> content, Pageable pageable) {
        Assert.notNull(content, "Content must not be null!");
        this.content.addAll(content);
        this.pageable = pageable;
    }

    @Override
    public int getNumber() {
        return this.pageable == null?0:this.pageable.getPageNumber();
    }

    @Override
    public int getSize() {
        return this.pageable == null?0:this.pageable.getPageSize();
    }

    @Override
    public int getNumberOfElements() {
        return this.content.size();
    }

    @Override
    public boolean hasPrevious() {
        return this.getNumber() > 0;
    }
    @Override
    public boolean isFirst() {
        return !this.hasPrevious();
    }
    @Override
    public boolean isLast() {
        return !this.hasNext();
    }
    @Override
    public Pageable nextPageable() {
        return this.hasNext()?this.pageable.next():null;
    }
    @Override
    public Pageable previousPageable() {
        return this.hasPrevious()?this.pageable.previousOrFirst():null;
    }
    @Override
    public boolean hasContent() {
        return !this.content.isEmpty();
    }
    @Override
    public List<T> getContent() {
        return Collections.unmodifiableList(this.content);
    }
    @Override
    public Sort getSort() {
        return this.pageable == null?null:this.pageable.getSort();
    }
    @Override
    public Iterator<T> iterator() {
        return this.content.iterator();
    }

    protected <S> List<S> getConvertedContent(Converter<? super T, ? extends S> converter) {
        Assert.notNull(converter, "Converter must not be null!");
        List<S> result = new ArrayList(this.content.size());
        Iterator var3 = this.iterator();

        while(var3.hasNext()) {
            T element = (T) var3.next();
            result.add(converter.convert(element));
        }

        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if(this == obj) {
            return true;
        } else if(!(obj instanceof com.common.Chunk)) {
            return false;
        } else {
            com.common.Chunk<?> that = (com.common.Chunk)obj;
            boolean contentEqual = this.content.equals(that.content);
            boolean pageableEqual = this.pageable == null?that.pageable == null:this.pageable.equals(that.pageable);
            return contentEqual && pageableEqual;
        }
    }
    @Override
    public int hashCode() {
        int result = 17;
        result = result + 31 * (this.pageable == null?0:this.pageable.hashCode());
        result += 31 * this.content.hashCode();
        return result;
    }
}
